package com.weex.app.extend;

import android.content.Context;
import android.os.Environment;
import android.os.StatFs;

import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

public class WxAndroidOs extends WXModule {

	public static String memory() {

		String str1 = "/proc/meminfo";// 系统内存信息文件
		String str2;
		String[] arrayOfString;
		long initial_memory = 0;

		try {

			FileReader localFileReader = new FileReader(str1);
			BufferedReader localBufferedReader = new BufferedReader(localFileReader, 8192);
			str2 = localBufferedReader.readLine();// 读取meminfo第一行，系统总内存大小

			arrayOfString = str2.split("\\s+");

			initial_memory = Integer.valueOf(arrayOfString[1]).intValue() / 1024;
			localBufferedReader.close();

		}catch (Exception e) {

		}

		return String.valueOf(initial_memory);
	}


    static public long diskSize() {
        //获取内部存储根目录
        File path = Environment.getDataDirectory();
        //系统的空间描述类
        StatFs stat = new StatFs(path.getPath());
        //每个区块占字节数
        long blockSize = stat.getBlockSize();
        //区块总数
        long totalBlocks = stat.getBlockCount();
        return totalBlocks * blockSize / 1024 / 1024;
    }

	public static String cpuName() {

		String result = "";
		String str2;

		try {

			FileReader fr = new FileReader("/proc/cpuinfo");
			BufferedReader localBufferedReader = new BufferedReader(fr);
			while ((str2=localBufferedReader.readLine()) != null) {
			  if (str2.contains("Hardware")) {
				result = str2.split(":")[1].trim();
				break;
			  }
			}
			localBufferedReader.close();

			fr.close();

		} catch (Exception e) {

		}

		return result;
	}


    @JSMethod(uiThread=false)
    public void meta(JSCallback callback) {
        Map<String, String> d = new HashMap<>();
        Context ctx = mWXSDKInstance.getContext();

        //获取设备序列号
        d.put("SERIAL", android.os.Build.SERIAL);

        // 获取系统版本字符串。如4.1.2 或2.2 或2.3等
        d.put("RELEASE", android.os.Build.VERSION.RELEASE);

        //获取设备制造商
        d.put("MANUFACTURER", android.os.Build.MANUFACTURER);

        //获取手机的型号 设备名称
        d.put("MODEL", android.os.Build.MODEL);

        //获取设备品牌
        d.put("BRAND", android.os.Build.BRAND);
        
        // 获取手机的型号 设备名称
        d.put("PRODUCT", android.os.Build.PRODUCT);

        // 获取设备语言
        d.put("LANGUAGE", Locale.getDefault().getLanguage());

        d.put("MEMORY", memory());
        
		d.put("CPU", cpuName());

		d.put("DISK", String.valueOf(diskSize()));

        d.put(
            "ANDROID_ID",
            android.provider.Settings.Secure.getString(
                ctx.getContentResolver(),
                android.provider.Settings.Secure.ANDROID_ID
            )
        );

        callback.invoke(d);
    }
    
}
